/**  
 * @Title: DataUtil.java
 * @Package org.csun.nc.utils
 * @Description: TODO
 * @author chisj chisj@foxmail.com
 * @date 2017年9月18日
 */
package com.csun.cmny.mqtt.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * ClassName: DataUtil 
 * @Description: TODO
 * @author chisj chisj@foxmail.com
 * @date 2017年9月18日
 */
public class DateUtil {
	
	public final static DateFormat YYYY_MM_DD_MM_HH_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public final static DateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
	
	public final static DateFormat YYYYMMDDMMHHSSSSS = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	
	public final static DateFormat YYYYMMDDHHMMSS = new SimpleDateFormat("yyyyMMddHHmmss");

	public static final DateFormat YYYYMMDD = new SimpleDateFormat("yyyyMMdd");
	
	public static final DateFormat MMDDHHMMSS =  new SimpleDateFormat("MMddHHmmss");
	
	public static final DateFormat HHMMSS = new SimpleDateFormat("HHmmss");
	
	public static String dateToStrs(Date date) {
		return HHMMSS.format(date);
	}
	
	/**
	 * 时间转换为yyyyMMddHHmmss格式
	 */
	public static String dateToStringss(Date date) {
		return MMDDHHMMSS.format(date);
	}
	
	/**
	 * 时间转换为yyyyMMddHHmmss格式
	 */
	public static String dateToStrings(Date date) {
		return YYYYMMDDMMHHSSSSS.format(date);
	}
	
	/**
	 * 时间转换为yyyy-MM-dd HH:mm:ss格式的字符串
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date){
		return YYYY_MM_DD_MM_HH_SS.format(date);
	}
	
	public static String dateToStr(Date date){
		return YYYY_MM_DD.format(date);
	}
	
	
	public static Date stringToDate(String dateString){
		Date date = null;
		try {
			date = YYYY_MM_DD.parse(dateString);
		} catch (ParseException e) {
			//System.out.println("dateString = " + dateString);
			e.printStackTrace();
		}
		return date;
	}
	
	
	public static Date strToDate(String dateString){
		Date date = null;
		try {
			date = YYYY_MM_DD_MM_HH_SS.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static Date strToYYMMDDDate(String dateString){
		Date date = null;
		try {
			date = YYYY_MM_DD.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 计算两个时间之间相差的天数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static long diffDays(Date startDate,Date endDate){
		long days = 0;
		long start = startDate.getTime();
		long end = endDate.getTime();
		//一天的毫秒数1000 * 60 * 60 * 24=86400000
		days = (end - start) / 86400000;
		return days;
	}
	
	/**
	 * 日期加上月数的时间
	 * @param date
	 * @param month
	 * @return
	 */
	public static Date dateAddMonth(Date date,int month){
		return add(date,Calendar.MONTH,month);
	}
	
	/**
	 * 日期加上天数的时间
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date dateAddDay(Date date,int day){
		return add(date,Calendar.DAY_OF_YEAR,day);
	}
	
	/**
	 * 日期加上年数的时间
	 * @param date
	 * @param year
	 * @return
	 */
	public static Date dateAddYear(Date date,int year){
		return add(date,Calendar.YEAR,year);
	}
	
	 /** 
     * 计算剩余时间 (多少天多少时多少分)
     * @param startDate
     * @param endDate
     * @return 
     */  
    public static String remainDateToString(Date startDate, Date endDate){  
    	StringBuilder result = new StringBuilder();
    	if(endDate == null ){
    		return "过期";
    	}
    	long times = endDate.getTime() - startDate.getTime();
    	if(times < -1){
    		result.append("过期");
    	}else{
    		long temp = 1000 * 60 * 60 *24;
    		//天数
    		long d = times / temp;

    		//小时数
    		times %= temp;
    		temp  /= 24;
    		long m = times /temp;
    		//分钟数
    		times %= temp;
    		temp  /= 60;
    		long s = times /temp;
    		
    		result.append(d);
    		result.append("天");
    		result.append(m);
    		result.append("小时");
    		result.append(s);
    		result.append("分");
    	}
    	return result.toString();
    }  
    
    
    /** 
     * 计算剩余时间 (多少天多少时多少分)
     * @param startDate
     * @param endDate
     * @return 
     */  
    public static String remainDateToString2(Date startDate, Date endDate){  
    	StringBuilder result = new StringBuilder();
    	if(endDate == null ){
    		return "竞拍结束";
    	}
    	long times = endDate.getTime() - startDate.getTime();
    	if(times < -1){
    		result.append("竞拍结束");
    	}else{
    		long temp = 1000 * 60 * 60 *24;
    		//天数
    		long d = times / temp;

    		//小时数
    		times %= temp;
    		temp  /= 24;
    		long m = times /temp;
    		//分钟数
    		times %= temp;
    		temp  /= 60;
    		long s = times /temp;
    		
    		result.append(d);
    		result.append("天");
    		result.append(m);
    		result.append("小时");
    		result.append(s);
    		result.append("分");
    	}
    	return result.toString();
    }  
    
    
	private static Date add(Date date,int type,int value){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(type, value);
		return calendar.getTime();
	}
	
	
	/**   
	 * @MethodName: getLinkUrl  
	 * @Param: DateUtil  
	 * flag ： true 转换  false 不转换
	 * @Author: gang.lv
	 * @Date: 2013-5-8 下午02:52:44
	 * @Return:    
	 * @Descb: 
	 * @Throws:
	*/
	public static String getLinkUrl(boolean flag,String content,String id){
		if(flag){
			content = "<a href='finance.do?id="+id+"'>"+content+"</a>";
		}
		return content;
	}
	
	/**
	 * 时间转换为时间戳
	 * @param format
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static long getTimeCur(String format,String date) throws ParseException{
		SimpleDateFormat sf = new SimpleDateFormat(format);
		return  sf.parse(sf.format(date)).getTime();
	}
	/**
	 * 时间转换为时间戳
	 * @param format
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static long getTimeCur(String format,Date date) throws ParseException{
		SimpleDateFormat sf = new SimpleDateFormat(format);
		return  sf.parse(sf.format(date)).getTime();
	}
	
	/**
	 * 将时间戳转为字符串 
	 * @param cc_time
	 * @return
	 */
	public static String getStrTime(String cc_time) { 
	 String re_StrTime = null; 
	 SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); 
	 long lcc_time = Long.valueOf(cc_time); 
	 re_StrTime = sdf.format(new Date(lcc_time * 1000L)); 
	 return re_StrTime; 
	 } 
	
	public static Timestamp strToTime(String date) {
		Calendar calendar = Calendar.getInstance();
		String dateStr = String.valueOf(calendar.get(Calendar.YEAR));
		dateStr = dateStr + date.substring(date.lastIndexOf("-")) + "-10";
		//System.out.println(dateStr);//得到年
		String curr_time_d = YYYY_MM_DD_MM_HH_SS.format(stringToDate(dateStr));
		Timestamp timestamp = Timestamp.valueOf(curr_time_d);
		return timestamp;
	}
	
	public static String getAge(String birthDay) throws Exception {
		
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		  Date date=new Date();
		  Date mydate= myFormatter.parse("2010-6-15");
		  long day=(date.getTime()-mydate.getTime())/(24*60*60*1000) + 1;
		  return new java.text.DecimalFormat("#").format(day/365);
		
	}
	
	// 获取当前周的周一
	public static String getCurrMon() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
		Calendar cld = Calendar.getInstance(Locale.CHINA); // 获取日历
		cld.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 设置日历当前时间为一周内周一的日期
		return sdf.format(cld.getTime()); // 获取日历时间
	}
	
	// 获取指定日期的周一
	public static String getMonByDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cld = Calendar.getInstance(Locale.CHINA);
		String imptimeBegin = "";
		try {
			cld.setTime(sdf.parse(date));
			int dayWeek = cld.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
			if (1 == dayWeek) {
				cld.add(Calendar.DAY_OF_MONTH, -1);
			}
			// System.out.println("要计算日期为:"+sdf.format(cld.getTime()));
			// //输出要计算日期
			cld.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
			int day = cld.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
			cld.add(Calendar.DATE, cld.getFirstDayOfWeek() - day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
			imptimeBegin = sdf.format(cld.getTime());
			// System.out.println("所在周星期一的日期："+imptimeBegin);
			cld.add(Calendar.DATE, 6);
			String imptimeEnd = sdf.format(cld.getTime());
			// System.out.println("所在周星期日的日期："+imptimeEnd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return imptimeBegin;
	}
	/** 计算年龄 */ 
	public static  String getAge(Date birthDay) throws Exception {
		Calendar cal = Calendar.getInstance();
		
		if (cal.before(birthDay)) {
			throw new IllegalArgumentException(
				"The birthDay is before Now.It's unbelievable!");
	        } 

	        int yearNow = cal.get(Calendar.YEAR); 
	        int monthNow = cal.get(Calendar.MONTH)+1; 
	        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); 
	        
	        cal.setTime(birthDay); 
	        int yearBirth = cal.get(Calendar.YEAR); 
	        int monthBirth = cal.get(Calendar.MONTH); 
	        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); 
            
	        int age = yearNow - yearBirth; 

	        if (monthNow <= monthBirth) { 
	            if (monthNow == monthBirth) { 
	                //monthNow==monthBirth 
	                if (dayOfMonthNow < dayOfMonthBirth) {
	                    age--; 
	                } 
	            } else { 
	                //monthNow>monthBirth 
	                age--; 
	            } 
	        } 

	        return age +""; 
	    }
	
	/** 计算年龄 */ 
	public static  String getMonth(Date birthDay) throws Exception {
		Calendar cal = Calendar.getInstance();
		
		if (cal.before(birthDay)) {
			throw new IllegalArgumentException(
				"The birthDay is before Now.It's unbelievable!"); 
	        } 

	        int yearNow = cal.get(Calendar.YEAR); 
	        int monthNow = cal.get(Calendar.MONTH)+1;
	        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); 
	        
	        cal.setTime(birthDay); 
	        int yearBirth = cal.get(Calendar.YEAR); 
	        int monthBirth = cal.get(Calendar.MONTH)+1;
	        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); 

	        int age = yearNow - yearBirth;
	        int month=monthNow-monthBirth;

	        if (monthNow <= monthBirth) {
	            if (monthNow == monthBirth) {
	                //monthNow==monthBirth 
	                if (dayOfMonthNow < dayOfMonthBirth) {
	                    age--; 
	                    month=11;
	                } else {
	                	
	                    month=0;
	                }
	            } else{ 
	                //monthNow>monthBirth 
	                age--; 
	                if (dayOfMonthNow >= dayOfMonthBirth){
	                	month=12-(monthBirth-monthNow);
	            } else{
	            	//System.out.println("44");
	            	month=12-(monthBirth-monthNow)-1;
	            }
	        }
	        }else{
	        	if (dayOfMonthNow >= dayOfMonthBirth){
                	month=monthNow-monthBirth;
                	}else{
                		month=monthNow-monthBirth-1;
                	}
	        }
	        //System.out.println("age = " + age);
	        if(age==0){
	        	if(monthNow < monthBirth){
	        	if(dayOfMonthNow >= dayOfMonthBirth){
	        		month=12-(monthBirth-monthNow);
	        	}else{
	        		month=11-(monthBirth-monthNow);
	        	}
	        	}else{
	        		//if(monthNow == monthBirth){
	        		//	month=11;
	        		//}
	        		
	        	}
	        }
	        String age_1="0";
	
	        	age_1=age + "岁"+month + "个月";
	        

	        return age_1;
	    }
}
